package com.easyfun.healthmagicbox.comm;

import java.io.IOException;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.http.client.ClientProtocolException;
import org.json.JSONException;
import org.json.JSONObject;

import android.content.Context;
import cn.kuaipan.android.openapi.KuaiPanManager;
import cn.kuaipan.android.openapi.KuaiPanTask;

import com.easyfun.healthmagicbox.base.DatabaseAccess;
import com.easyfun.healthmagicbox.base.DatabaseAccessImplementation;
import com.easyfun.healthmagicbox.comm.json.PictureJsonWrapper;
import com.easyfun.healthmagicbox.ormlite.DatabaseHelper;
import com.easyfun.healthmagicbox.ormlite.ServerResponseCode;
import com.easyfun.healthmagicbox.pojo.ConstantData;
import com.easyfun.healthmagicbox.pojo.Picture;
import com.easyfun.healthmagicbox.sync.CommCallback;
import com.j256.ormlite.dao.Dao;

public class PicturesUploadTask extends BaseRedoTask implements KuaiPanTask,DatabaseAccess{
	Picture pic;
	
	public PicturesUploadTask(Context context, CommCallback callback) {
		super(context, callback);
	}
	public PicturesUploadTask(Context context, CommCallback callback,
			Picture pic) {
		super(context, callback);
		this.pic = pic;
	}

	@Override
	public void run() {
		String personID = pic.getPersonID();
		if ((personID==null)||(personID.length()==0))
			return;

		//KuaiPanManager.getInstance(mcontext).mkdir(this);
		KuaiPanManager.getInstance(mcontext).uploadFile(this);
		
	}

	@Override
	public String serializeParam() {
		return pic.getPersonID()+BaseRedoTask.ContentsSeperator
				+pic.getFilename()+BaseRedoTask.ContentsSeperator
				+pic.getCreated();
	}

	@Override
	public void deserializeParam(String serialstring) throws IllegalArgumentException{
		String[] tmp = serialstring.split(BaseRedoTask.regExHead + BaseRedoTask.ContentsSeperator);
		if (pic == null)
		{
			pic = new Picture();
		}
		if (tmp.length == 3)
		{
			pic.setPersonID(tmp[0]);
			pic.setFilename(tmp[1]);
			pic.setCreated(tmp[2]) ;
			return;
		}else
		{
			throw new IllegalArgumentException("param length is not 3");
		}
		
	}
	public String getFilename()
	{
		return pic.getFilename();
	}
	public String getPersonID()
	{
		return pic.getPersonID();
	}
	
	public CommCallback getCommCallback()
	{
		return ccb;
	}
	
	@Override
	public void successfulRun(Object ob) {
		PictureJsonWrapper<PictureJsonWrapper,?> wrapper = 
				new PictureJsonWrapper(pic);		
		
		Map<String, String> parmas = new HashMap<String, String>();
	     try {
			parmas.put("jsonstring", wrapper.serialize());
		} catch (JSONException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
	     
		String result = "";
		try {
			result = super.uploadByHTTP(parmas,ConstantData.PictureUploadURI);
		} catch (ClientProtocolException e) {
			e.printStackTrace();
			ccb.setCommResult(this, 
					"", ServerResponseCode.FAILED);
			return;
		} catch (IOException e) {
			e.printStackTrace();
			ccb.setCommResult(this, 
					"", ServerResponseCode.FAILED);
			return;
		}
		
		if ((ServerResponseCode.parseOf(result)==ServerResponseCode.FAILED)
				||(ServerResponseCode.parseOf(result)==ServerResponseCode.NOMORE))
		{
			ccb.setCommResult(this, 
					"", ServerResponseCode.FAILED);			
		}else if (ServerResponseCode.parseOf(result)==ServerResponseCode.SUCCESS)
		{
			ccb.setCommResult(this, 
					"", ServerResponseCode.SUCCESS);	
		}else
		{
			int syncID = 0;
			try{
				syncID = Integer.valueOf(result);
			} catch (Exception e) {
				ccb.setCommResult(this, 
						"", ServerResponseCode.FAILED);	
				e.printStackTrace();
				return;
			}
			try {
				Dao<Picture, Integer> dao = getHelper(mcontext).getPictureDao();
				List<Picture> pics = dao.queryForEq("filename",pic.getFilename());
				if (pics.size() == 1)
				{
					Picture oldpic = pics.get(0);
					oldpic.setSyncid(syncID);
					dao.createOrUpdate(oldpic);
				}
				ccb.setCommResult(this, 
						"", ServerResponseCode.SUCCESS);	
			} catch (SQLException e) {
				ccb.setCommResult(this, 
					"", ServerResponseCode.FAILED);	
				e.printStackTrace();
			}
		}
		
	}
	@Override
	public void failedRun(Object ob) {
		ccb.setCommResult(this, 
				"", ServerResponseCode.FAILED);
		
	}
	@Override
	public DatabaseHelper getHelper(Context context) {
		return DatabaseAccessImplementation.getInstance().getHelper(context);
	}

}
